package com.ssfeng.youxia.controller;

import com.ssfeng.youxia.service.ISessionsService;
import com.ssfeng.youxia.services.EnvironmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.List;
import java.util.Map;

/**
 * Created by liyuhang on 2019-09-15.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private MappingJackson2JsonView mappingJackson2JsonView;


    @Autowired
    private ISessionsService sessionsService;

    @Autowired
    private EnvironmentService environmentServicea;

    @RequestMapping(value = "/getOnlineUser")
    public ModelAndView getOnlineUser(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView(mappingJackson2JsonView);
        List<Map<String, Object>> list = sessionsService.getOnlineUsers();
        modelAndView.addObject("data", list);
        return modelAndView;
    }


    @RequestMapping(value = "/kickOutUser")
    public ModelAndView kickOutUser(String id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView(mappingJackson2JsonView);
        modelAndView.addObject("data", "成功");
        sessionsService.kickOutUser(id);
        return modelAndView;
    }

    @GetMapping(value = "/test")
    @ResponseBody
    public Object test(){
        System.out.println("hello");
        return "hello";
    }
}
